import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import classNames from "classnames";
import ContextMenu from "devextreme-react/context-menu";
import CpasIcon from "../../components/Icon";
/**
 * ~~~js
 * 默认二封增强树组件
 * ~~~~
 */
export default function DevTreeDefaultHoc(WrappedComponent) {
    // 泛型限定
    return class extends WrappedComponent {
        parent = super.render();
        dataSource;
        defaultConfig = {
            searchEnabled: true,
            searchMode: "contains",
        };
        treeRef;
        contextMenuRef;
        get treeInstance() {
            return this.treeRef.current.instance;
        }
        get contextMenu() {
            return this.contextMenuRef.current.instance;
        }
        constructor(...props) {
            super(...props);
            this.treeRef = React.createRef();
            this.contextMenuRef = React.createRef();
            this.treeViewItemContextMenu = this.treeViewItemContextMenu.bind(this);
            this.contextMenuItemClick = this.contextMenuItemClick.bind(this);
            this.renderTreeViewItem = this.renderTreeViewItem.bind(this);
            this.handleItemDblClick = this.handleItemDblClick.bind(this);
        }
        componentDidMount() { }
        componentWillUnmount() {
            console.log("卸载事件");
            this.treeInstance.dispose();
        }
        treeViewItemContextMenu(e) {
            console.log(e);
        }
        contextMenuItemClick(e) {
            //如果调用方自己监听了该事件， 则抛给调用者实现
            if (this.props.contextMenuItemClick) {
                this.props.contextMenuItemClick(e);
            }
            else if (e.itemData.onItemClick) {
                e.itemData.onItemClick(e);
            }
        }
        handleItemDblClick = (e) => {
            // 双击事件的处理逻辑
            e.preventDefault();
            if (this.props.onItemDblClick) {
                this.props.onItemDblClick(e);
            }
            else {
                console.log("双击事件");
            }
        };
        renderTreeViewItem(item) {
            let value;
            if (this.props.itemRender)
                value = this.props.itemRender(item);
            const displayExpr = this.props.displayExpr;
            if (!value)
                value = item[displayExpr];
            const icon = item["icon"];
            return (_jsxs("div", { className: "dev-tree-item-render", children: [icon && _jsx(CpasIcon, { type: icon, fontSize: 18 }), _jsx("span", { children: value })] }));
        }
        render() {
            const klass = classNames("cpas-grid", "PaperDevSortableGrid");
            const { id, ...rest } = this.props;
            const target = `#${id} .dx-treeview-item`;
            const menuItems = [
            /**20231025 打开数据量大的树结构时非常慢暂时注释掉 */
            // {
            //   text: "展开所有",
            //   onItemClick: (e) => {
            //     this.treeInstance.expandAll();
            //   },
            // },
            // {
            //   text: "收起所有",
            //   onItemClick: (e) => {
            //     this.treeInstance.collapseAll();
            //   },
            // },
            ];
            let menu;
            if (rest.ContextMenu) {
                menu = [...menuItems, ...rest.ContextMenu];
            }
            else {
                menu = [...menuItems];
            }
            let props = {
                className: klass,
                ...this.defaultConfig,
                ...rest,
                itemRender: this.renderTreeViewItem,
                ref: this.treeRef,
            };
            return (_jsxs("div", { onDoubleClick: this.handleItemDblClick, style: { height: "100%" }, children: [React.cloneElement(this.parent, { ...props }), _jsx(ContextMenu, { ref: this.contextMenuRef, dataSource: menu, target: target, onItemClick: this.contextMenuItemClick })] }));
        }
    };
}
